package com.hkts.naturalgas.utils;


import com.hkts.naturalgas.config.HttpClient;
import com.hkts.naturalgas.config.HttpConnectionManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @ClassNameHttpAPIService
 * @Description
 * @Author yck
 * @Date2021-06-11 10:45
 * @Version V1.0
 **/

@Service
@Slf4j
public class HttpAPIService {

    @Autowired
    private CloseableHttpClient httpClient;

    @Autowired
    private RequestConfig config;

    @Autowired
    private PoolingHttpClientConnectionManager poolingHttpClientConnectionManager;


    /**
     * 不带参数的get请求，如果状态码为200，则返回body，如果不为200，则返回null
     *
     * @param url
     * @return
     * @throws Exception
     */
    public String doGet(String url) throws Exception {
        StringBuffer sb = new StringBuffer ();
        org.apache.http.client.HttpClient client = null;

        InputStream in = null;
        InputStreamReader isr = null;
        HttpGet get = new HttpGet();
        try
        {
            client=  HttpConnectionManager.getHttpClient();
            get.setURI(new URI(url));
            HttpResponse response = client.execute(get);
            if (response.getStatusLine ().getStatusCode () != 200) {
                get.abort();
                return null;
            }
            HttpEntity entity =response.getEntity();

            if( entity != null ){
                in = entity.getContent();
            }
            if(response.getStatusLine ().getStatusCode () == 200){
                // 返回响应体的内容
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            }
            return sb.toString ();

        }
        catch (Exception e)
        {
            get.abort();
            e.printStackTrace ();
            return null;
        }
        finally
        {
            if (isr != null){
                try
                {
                    isr.close ();
                }
                catch (IOException e)
                {
                    e.printStackTrace ();
                }
            }
            if (in != null){
                try
                {
                    in.close ();
                }
                catch (IOException e)
                {
                    e.printStackTrace ();
                }
            }
        }
    }

    /**
     * 带参数的get请求，如果状态码为200，则返回body，如果不为200，则返回null
     *
     * @param url
     * @return
     * @throws Exception
     */
    public String doGet(String url, Map<String, Object> map) throws Exception {
        URIBuilder uriBuilder = new URIBuilder(url);

        if (map != null) {
            // 遍历map,拼接请求参数
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
            }
        }

        // 调用不带参数的get请求
        return this.doGet(uriBuilder.build().toString());

    }



    /**
     * 不带参数post请求
     *
     * @param url
     * @return
     * @throws Exception
     */
/*    public HttpResult doPost(String url) throws Exception {

        return null;
    }*/
}
